#include <bits/stdc++.h>

using namespace std;
int degree[1001],visited[1001],N,M;
vector<int> v[1001],tempPath; //邻接表
set<vector<int>> result;

void dfs(){
    if((int)tempPath.size()==N) {
        result.insert(tempPath);
        return;
    }
    for(int i=1;i<=N;i++){
        if(!visited[i] && degree[i]==0){
            visited[i] = 1;
            tempPath.push_back(i);
            for(int j=0;j<(int)v[i].size();j++) degree[v[i][j]]--;
            dfs();
            visited[i] = 0;
            for(int j=0;j<(int)v[i].size();j++) degree[v[i][j]]++;
            tempPath.pop_back();
        }
    }
}

int main(){
    freopen("../A1146.txt","r",stdin);
    scanf("%d %d",&N,&M);

    int a,b;
    while(M--){
        scanf("%d %d",&a,&b);
        v[a].push_back(b);
        degree[b]++;
    }
    dfs();

    int K;
    scanf("%d",&K);
    vector<int> temp(N);
    vector<int> out;
    for(int i=0;i<K;i++){
        for(int j=0;j<N;j++) scanf("%d",&temp[j]);
        if(result.find(temp)==result.end()) out.push_back(i);
    }

    for(int i=0;i<(int)out.size();i++){
        if(i) printf(" ");
        printf("%d",out[i]);
    }
}